package cn.kgc.controller;

import cn.kgc.pojo.Branch;
import cn.kgc.pojo.Job;
import cn.kgc.pojo.position;
import cn.kgc.service.JobService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class JobController {

    @Resource
    private JobService jobService;

    @RequestMapping("/wangzwfbgl")
    public String index() {
        return "wangzwfbgl";
    }

    @RequestMapping("/wangzwfbdj")
    public String tianJia() {
        return "wangzwfbdj";
    }

    @RequestMapping("/wangwfbxg")
    public String xiuGai() {
        return "wangwfbxg";
    }

    @RequestMapping("/wangzzwfbmx")
    public String mingXi() {
        return "wangwfbmx";
    }

    @RequestMapping("/page222")
    @ResponseBody
    public String page(
            @RequestParam int posId,
            @RequestParam String posType,
            @RequestParam String startTime,
            @RequestParam String endTime,
            @RequestParam int currentPage,
            @RequestParam int pageSize
    ) {
        PageHelper.startPage(currentPage, pageSize);
        Page<Job> page = jobService.page(posId, posType, startTime, endTime);
        PageInfo<Job> pageInfo = new PageInfo<Job>(page);
        System.out.println(pageInfo);
        return JSONObject.toJSONStringWithDateFormat(pageInfo, "yyyy-MM-dd");
    }

    @RequestMapping("/selPosName")
    @ResponseBody
    public String selPosName() {
        List<position> positionList = jobService.select();
        return JSONObject.toJSONString(positionList);
    }


    @RequestMapping("/addJob")
    @ResponseBody
    public Boolean addJob(@RequestParam int brId,
                          @RequestParam String joType,
                          @RequestParam int joCount,
                          @RequestParam int posId,
                          @RequestParam String registrant,
                          @RequestParam String startTime,
                          @RequestParam String endTime,
                          @RequestParam String jobDescribe,
                          @RequestParam String jobRequire) {
        boolean add = jobService.add(brId, joType, joCount, posId, registrant, startTime, endTime, jobDescribe, jobRequire);
        if (add) {
            return true;
        }
        return false;
    }


    @RequestMapping("/selectBr")
    @ResponseBody
    public String selectBr() {
        List<Branch> branchList = jobService.selectBr();
        return JSONObject.toJSONString(branchList);
    }


    @RequestMapping("/selectJobById/{id}")
    public String selJobById(@PathVariable int id, Model model) {
        System.out.println(id + "==============================================================");
        Job jobs = jobService.selectById(id);
        System.out.println("jobs ============================== " + jobs);
        model.addAttribute("jobs", jobs);
        List<Branch> branchList = jobService.selectBr();
        List<position> positionList = jobService.select();
        model.addAttribute("branchList", branchList);
        model.addAttribute("positionList", positionList);
        return "/wangwfbxg";
    }


    @RequestMapping("/selectJobById2")
    public String selJobById(@RequestParam int id) {
        Job jobs = jobService.selectById(id);
        System.out.println("jobs ============================== " + jobs);


        return JSON.toJSONStringWithDateFormat(jobs, "yyyy-MM-dd");
    }


    @RequestMapping("/updJob")
    @ResponseBody
    public String updJob(
            int id,
            int brId,
            String joType,
            int joCount,
            int posId,
            String registrant,
            String startTime,
            String endTime,
            String jobDescribe,
            String jobRequire) {
        boolean updJob = jobService.updJob(id, brId, joType, joCount, posId, registrant, startTime, endTime, jobDescribe, jobRequire);
        return JSON.toJSONStringWithDateFormat(updJob, "yyyy-MM-dd");
    }

    @RequestMapping("/selectMx/{id}")
    public String selectMx(@PathVariable int id, Model model) {
        Job jobs = jobService.selectById(id);
        model.addAttribute("jobs", jobs);
        return "wangzwfbmx";
    }


    @RequestMapping("/deljob")
    @ResponseBody
    public String  deljob(@RequestParam int id) {
        boolean delJob = jobService.delJob(id);
        return JSON.toJSONString(delJob);
    }
}
 